import { BaseDeployPanel } from "../../../equipment/deploy/panel/BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { GImageBitmap } from "../../../../../../com/mui/controls/GImageBitmap";
import { DeployPanelItem } from "../../../equipment/deploy/DeployPanelItem";
import { PetEquipStars } from "../PetEquipStars";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { Alert } from "../../../../../../com/mui/controls/Alert";
import { MsgManager } from "../../../../manager/MsgManager";
import { ItemsUtil } from "../../../shortcuts/item/ItemsUtil";
import { DataEvent } from "../../../../events/DataEvent";
import { EquipmentModuleConst } from "../../../equipment/data/EquipmentModuleConst";
import { EquipUpdateParams } from "../../../equipment/data/EquipUpdateParams";
import { EBind } from "../../../../../../Message/Public/EBind";
import { ItemData } from "../../../../resource/info/ItemData";
import { Dispatcher } from "../../../../../mvc/core/Dispatcher";
import { EventName } from "../../../../mvc/EventName";
import { PetEquipForgeConfig } from "../../../../resource/PetEquipForgeConfig";
import { Cache } from "../../../../cache/Cache";
import { PetEquipGuideWindow } from "../../PetEquipGuideWindow";
import { ImagesConst } from "../../../../resource/ImagesConst";
import { SystemSetter } from "../../../system/SystemSetter";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { ItemExInfo } from "../../../../resource/info/ItemExInfo";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
import { SWFPlayer } from "../../../../scene/modle/SWFPlayer";
import { ModelType } from "../../../../scene/modle/data/ModelType";
import { ModelInfo } from "../../../../scene/player/info/ModelInfo";
type int = number;
//class PetEquipUpgradeStarPanel
    
    export  class PetEquipUpgradeStarPanel extends BaseDeployPanel
    {
        constructor()
        {
            this.posData = [{"x": 95, "y": 180, "label": Language.getString(60230), "canRemove": true, "noBoarder": false}, {"x": 204, "y": 180, "label": Language.getString(60231), "canRemove": false, "noBoarder": true}, {"x": 45, "y": 80, "label":"", "canRemove": false, "noBoarder": false}];
            this.buttonLabel = Language.getString(61318);
            this._alarmText = Language.getString(60147);
            this._useItems = [];
            super();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc4=null;
            var loc5=null;
            super.createChildren();
            var loc1=new GImageBitmap("equipment_recast_recast.png", 150, 180);
            this.addChild(loc1);
            var loc2=new GImageBitmap("Label_jipingyulang.png", 16, 122);
            this.addChild(loc2);
            var loc3=0;
            while (loc3 < this.posData.length) 
            {
                loc4 = this.posData[loc3];
                (loc5 = new DeployPanelItem(loc3, loc4.noBoarder)).posIndex = loc3;
                loc5.x = loc4["x"];
                loc5.y = loc4["y"];
                loc5.label = loc4["label"];
                if (loc4.canRemove) 
                {
                    loc5.mouseEnabled = true;
                    loc5.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                addChild(loc5);
                allItem.push(loc5);
                ++loc3;
            }
            this._equipStars = new PetEquipStars();
            this._equipStars.x = 60;
            this._equipStars.y = 250;
            this._equipStars.starMode = PetEquipStars.StarBig;
            this.addChild(this._equipStars);
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 140;
            confirmButton.y = 305;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            return;
        }

        /* internal  */onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            this.moveOutByIndex(loc1.posIndex);
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.onButtonClickImpl(arg1.currentTarget);
            return;
        }

        /* internal  */onButtonClickImpl(arg1): void
        {
            var loc1=null;
            if (!this.isCanOperate()) 
            {
                return;
            }
            var loc2=arg1;
            switch (loc2) 
            {
                case confirmButton:
                {
                    if (this.checkIsChangeToBind()) 
                    {
                        loc1 = this._alarmText;
                        Alert.show(loc1, null, Alert.OK | Alert.CANCEL, null, this.closeConfirmAlertHandler);
                    }
                    else 
                    {
                        this.onConfirm();
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */isCanOperate(): Boolean
        {
            if (allItem[0].itemData == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60234));
                return false;
            }
            if (allItem[0].itemData && allItem[0].itemData.itemExInfo.p_equip_star >= 10) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61326));
                return false;
            }
            if (allItem[1].itemData == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61324));
                return false;
            }
            if (allItem[1].itemData && !allItem[1].enabled) 
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(61325, ItemsUtil.getItemName(allItem[1].itemData)));
                return false;
            }
            if (!isEnoughMoney) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60121));
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                return false;
            }
            return true;
        }

        /* internal  */closeConfirmAlertHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.onConfirm();
            }
            return;
        }

        /* internal  */onConfirm(): void
        {
            var loc4=null;
            var loc5=null;
            var loc1=allItem[0].itemData.posType;
            var loc2=allItem[0].itemData.uid;
            var loc3=[];
            var loc6=0;
            var loc7=this._useItems;
            for(loc4 of loc7) 
            {
                loc3.push(loc4.uid);
            }
            (loc5 = new EquipUpdateParams()).operationId = this.operationId;
            loc5.equipPosType = loc1;
            loc5.equipUid = loc2;
            loc5.materials = loc3;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc5, false, true));
            return;
        }

        /* internal  */checkIsChangeToBind(): Boolean
        {
            if (!(allItem[0].itemData.itemExInfo.bind == EBind._EBindYes) && allItem[1].itemData.itemInfo.item.bind == EBind._EBindYes) 
            {
                return true;
            }
            return false;
        }

        /* internal  */onAlertCloseHandler(arg1: int): void
        {
            var loc1=null;
            if (arg1 == Alert.OK) 
            {
                loc1 = allItem[0].itemData as ItemData;
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetEquipUpgradeGuide, loc1));
            }
            this._AlertHasShow = false;
            return;
        }

        protected /* override */ updateStatus(): void
        {
            var loc2=0;
            var loc3=null;
            var loc4=null;
            var loc1=0;
            if (allItem[0].itemData) 
            {
                loc1 = PetEquipForgeConfig.instance.getCost(1, allItem[0].itemData.color);
                loc2 = allItem[0].itemData.itemExInfo.p_equip_star;
                this._equipStars.starNum = loc2;
                loc3 = Cache.instance.clientSettingCache;
                if (loc3.isFirstTimeUpgradeTenStar == false && PetEquipGuideWindow.instance.isHide && loc2 >= 10 && this._AlertHasShow == false && Cache.instance.equipment.equipColorMoreThanGreen == false) 
                {
                    this._AlertHasShow = true;
                    Alert.okLabel = Language.getString(61352);
                    Alert.buttonWidth = 75;
                    Alert.show(Language.getString(61356), Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onAlertCloseHandler);
                }
                else if (loc3.isFirstTimeUpgradeTenStar && loc2 >= 10 && Cache.instance.equipment.equipColorMoreThanGreen == false) 
                {
                    if ((loc4 = PetEquipGuideWindow.instance as PetEquipGuideWindow).isHide) 
                    {
                        loc4.itemData = allItem[0].itemData as ItemData;
                        loc4.loadBG(ImagesConst.PetEquipUpgradeGuide);
                        loc4.updateDisplayListByType(PetEquipGuideWindow.PET_EQUIP_UPGRADE_GUIDE);
                    }
                    if (loc3.isFirstTimeUpgradeTenStar) 
                    {
                        loc3.isFirstTimeUpgradeTenStar = false;
                        SystemSetter.systemGuideSetterInfo.setSystemInfo("isFirstTimeUpgradeTenStar", false);
                        Dispatcher.dispatchEvent(new DataEvent(EventName.SaveGuideSystemSetter));
                    }
                }
            }
            else 
            {
                loc1 = 0;
                allItem[2].itemData = null;
                this._equipStars.starNum = 0;
            }
            if (_cost != loc1) 
            {
                _cost = loc1;
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            return;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            if (arg1.itemInfo.item.category == ECategory._ECategoryPetEquip) 
            {
                allItem[0].itemData = arg1;
                allItem[2].itemData = this.getHighestStarEquip();
                this.updateStatus();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [arg1], false, true));
            }
            return;
        }

        /* internal  */getHighestStarEquip(): ItemData
        {
            if (!allItem[0].itemData) 
            {
                return null;
            }
            var loc1=allItem[0].itemData;
            var loc2=new ItemData(loc1.itemCode);
            var loc3=new ItemExInfo();
            if (loc1.itemExInfo) 
            {
                loc3 = loc1.itemExInfo.duplicate();
            }
            loc3.p_equip_star = 10;
            loc2.itemExInfo = loc3;
            return loc2;
        }

        public moveOutByIndex(arg1: int): void
        {
            if (arg1 >= 0 && arg1 < 2) 
            {
                if (arg1 != 0) 
                {
                    allItem[arg1].itemData = null;
                    this.updateStatus();
                }
                else 
                {
                    this.clear();
                    allItem[2].itemData = null;
                }
            }
            return;
        }

        public /* override */ clear(): void
        {
            clearAllItem();
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            return;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            this._useItems = arg1.material as Array<any>;
            if (this._useItems.length > 0) 
            {
                allItem[1].itemData = this._useItems[0] as ItemData;
                allItem[1].enabled = arg1.isEnough;
                allItem[1].amount = arg1.amountNeed;
            }
            else 
            {
                allItem[1].itemData = null;
            }
            this.updateStatus();
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc5=0;
            var loc6=0;
            var loc1=new ItemData(arg3);
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61328));
                this.clear();
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(61327));
                this.clear();
                if (loc1.itemExInfo) 
                {
                    loc2 = "equipEffect_petEquipStar.swf";
                    loc3 = loc1.itemExInfo.p_equip_star;
                    loc6 = 0;
                    while (loc6 < loc3) 
                    {
                        loc4 = this._equipStars.x + loc6 * 23 + 11;
                        loc5 = 261;
                        this.addStarEffect(loc2, loc4, loc5);
                        ++loc6;
                    }
                }
            }
            this.putInItem(loc1);
            return;
        }

        /* internal  */addStarEffect(arg1: string, arg2: int, arg3: int): void
        {
            var loc1;
            (loc1 = new SWFPlayer()).sceneX = arg2;
            loc1.sceneY = arg3;
            loc1.framesPlayerCompleteHandler = this.onEffectPlayComplete;
            loc1.load(arg1, ModelType.Effect, new ModelInfo());
            this.addChild(loc1);
            return;
        }

        /* internal  */onEffectPlayComplete(arg1: SWFPlayer): void
        {
            arg1.dispose();
            return;
        }

        /* internal */ /* var */operationId: int=15;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */buttonLabel: string;

        /* internal */ /* var */_equipStars: PetEquipStars;

        /* internal */ /* var */_alarmText: string;

        /* internal */ /* var */_useItems: Array<any>;

        /* internal */ /* var */_AlertHasShow: Boolean;
    }
